﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using My.Admin.Dto;
using My.Admin.Dto.System;
using My.Admin.Service.System;
using My.Core.Attributes;
using My.Core.DynamicWebApi.Attributes;

namespace My.Admin.Dynamic
{
	/// <summary>
	/// 动态API使用示例
	/// </summary>
	[Service("AppleAppService")]
	public class AppleAppService
	{
		private static readonly Dictionary<int, string> Apples = new Dictionary<int, string>()
		{
			[1] = "Big Apple",
			[2] = "Small Apple"
		};

		/// <summary>
		/// Get An Apple.
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		[HttpGet("{id:int}")]
		[Authorize]
		public string Get(int id)
		{
			if (Apples.ContainsKey(id))
			{
				return Apples[id];
			}
			else
			{
				return "No Apple!";
			}
		}

		/// <summary>
		/// Get All Apple.
		/// </summary>
		/// <returns></returns>
		//[NotApiResult]
		public IEnumerable<string> Get()
		{
			return Apples.Values;
		}

		/// <summary>
		/// 更新
		/// </summary>
		/// <param name="dto"></param>
		public void Update(UpdateAppleDto dto)
		{
			if (Apples.ContainsKey(dto.Id))
			{
				Apples[dto.Id] = dto.Name;
			}
		}

		/// <summary>
		/// Delete Apple
		/// </summary>
		/// <param name="id">Apple Id</param>
		[HttpDelete("{id:int}")]
		public void Delete(int id)
		{
			if (Apples.ContainsKey(id))
			{
				Apples.Remove(id);
			}
		}

	}
}
